package yl.hs.bmipfull.controllers.vehiclemanage;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.vehiclemanage.TruckInfoEntity;
import yl.hs.bmipfull.service.laboratory.IProductOverageService;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.service.production.IProductionTaskService;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.service.vehiclemanage.ITruckInfoService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作用:  车辆信息控制层<br/>
 * 创建时间: 2021年05月20日 16时10分49秒 <br/>
 *
 * @author 谢东
 */
@Api(value = "车辆信息Web接口", tags = "运输管理——车辆信息操作")
@RequestMapping(value = "/api/truckInfo")
@RestController
public class TruckInfoController extends ControllerBase {

    @Autowired
    ITruckInfoService service;
    @Autowired
    ITransTaskService Taskservice;
    @Autowired
    IProductOverageService Overageservice;
    @Autowired
    IProductionTaskService ProductTaskservice;
    @Autowired
    IProductShipOrderService shipOrderService;
    @Autowired
    IProductionLineService lineservice;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询车辆信息", notes = "查询车辆信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询车辆信息", notes = "查询车辆信息")
    @RequestMapping(value = "/Dispatch/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryListByDispatch(@RequestBody PageQuery query,
                                         @ApiParam(value = "页码") @PathVariable int page,
                                         @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            List<Map<String, Object>> overMap = new ArrayList<>();
            svSVC.updateUserMenuView(query);
            var result = service.queryPage(query, rowNum, page);
            for (TruckInfoEntity entity : result.getData()) {
                var entityMap = entity.toMap();
                var Overage = Overageservice.findByState(entity.getSn());
                if (Overage.size() > 0) {
                    if (Overage.get(0) != null) {
                        entityMap.put("Overage", Overage.get(0));
                    }
                } else {
                    entityMap.put("Overage", null);
                }
                overMap.add(entityMap);
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("page", result.getPage());
            resultMap.put("records", result.getRecords());
            resultMap.put("total", result.getTotal());
            resultMap.put("data", overMap);
            resultMap.put("pageDataSize", result.pageDataSize);
            return success("查询成功", resultMap);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询车辆信息", notes = "根据车辆状态查询车辆信息")
    @RequestMapping(value = "/select/{state}/{useType}", method = RequestMethod.GET)
    public WebResult queryList(@ApiParam(value = "状态") @PathVariable short state, @ApiParam(value = "车辆类型") @PathVariable String useType) {
        try {
            return success("查询成功", service.selectByState(state, useType));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询车辆信息", notes = "根据车辆状态查询车辆信息")
    @RequestMapping(value = "/selectMortarTruck", method = RequestMethod.GET)
    public WebResult queryMortarTruckList() {
        try {
            return success("查询成功", service.queryMortarTruck());
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询车辆信息", notes = "分页查询车辆信息")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询车辆信息", notes = "根据车辆信息编号查询车辆信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }

    @ApiOperation(value = "查询全部车辆信息", notes = "根据全部车辆信息")
    @RequestMapping(value = "/findall", method = RequestMethod.GET)
    public WebResult findAll() {
        return success("查询成功", service.findAll());
    }
    @ApiOperation(value = "保存车辆信息", notes = "查询/更新车辆信息信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveTruckInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除车辆信息信息", notes = "根据车辆信息编号删除系车辆信息信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "车辆信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }

    @ApiOperation(value = "根据车辆编号修改车辆状态", notes = "根据车辆编号修改车辆状态")
    @RequestMapping(value = "/{state}", method = RequestMethod.POST)
    public WebResult updateStateBySn(@ApiParam(value = "车辆编号") @RequestParam("sn") String[] sn, @ApiParam(value = "车辆状态") @PathVariable short state) {
        try {
            return success("查询成功", service.changeStateBySn(sn, state));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询车辆信息，派车信息", notes = "查询车辆信息，派车信息")
    @RequestMapping(value = "/queryall/{useType}/{cateSN}", method = RequestMethod.GET)
    public WebResult queryAll(@ApiParam(value = "车辆类型") @PathVariable String useType, @ApiParam(value = "产品类别") @PathVariable String cateSN) {

        return success("查询成功", service.queryAll(useType, cateSN));
    }

    @ApiOperation(value = "车辆信息验证唯一性", notes = "车辆信息验证唯一性")
    @RequestMapping(value ={"/verification/{key}/{value}/{sn}","/verification/{key}/{value}"} , method = RequestMethod.GET)
    public WebResult truckInfoVerification(@ApiParam(value = "字段名") @PathVariable String key, @ApiParam(value = "字段值") @PathVariable String value,@ApiParam(value = "编号") @PathVariable(required=false) String sn) {
        Integer result=0;
        if(sn==null || sn.equals("null") ||sn.equals("")) {
            result=service.truckInfoVerification(key, value,null);
        }else{
            result=service.truckInfoVerification(key, value,sn);
        }
        return success("查询成功", result);
    }
    @ApiOperation(value = "根据卡号获取车辆信息",notes = "根据卡号获取车辆信息")
    @RequestMapping(value = "/findbycardsn/{cardSNO}",method = RequestMethod.GET)
    public WebResult findByCardSn(@ApiParam(value = "车辆卡号") @PathVariable String cardSNO){
        return success("成功",service.findByCardSn(cardSNO));
    }
    @ApiOperation(value = "恢复车辆信息信息", notes = "根据车辆信息编号恢复系车辆信息信息")
    @RequestMapping(path = "/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    public WebResult recovery(@ApiParam(value = "车辆信息编号", name = "sn", allowMultiple = true, required = true) @PathVariable String[] sn,@ApiParam(value = "车辆状态", name = "state", allowMultiple = true, required = true) @PathVariable short state) {
        if (service.recovery(sn,state) > 0) {
            return success("操作成功");
        } else {
            return fail("操作失败");
        }

    }

    @RequestMapping(value = "backByCard/{carNum}",method = RequestMethod.GET)
    public WebResult truckBackByCardNum(@ApiParam(value = "车辆卡号") @PathVariable String carNum){
        try {
            var truck = service.findByCardSn(carNum);
            return success("ok",service.truckBack(truck));
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @RequestMapping(value = "backByPlate/{plate}",method = RequestMethod.GET)
    public WebResult truckBackByPlate(@ApiParam(value = "车辆卡号") @PathVariable String plate){
        try {
            var truck = service.findByTruck(plate);
            return success("ok",service.truckBack(truck));
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @RequestMapping(value = "/findStateAll",method = RequestMethod.GET)
    public WebResult findStateAll(){
        var stateAll = service.findStateAll();
        return success("查询成功",stateAll);
    }


}